package ink.lovejinhu.leetcode;

import ink.lovejinhu.common.ListNode;

/**
 * @author jinhu
 * created 2021-06-30 9:48
 */
public class Solution_0141 {
    /**
     * 判断一个链表是否有环
     *
     * @param head
     * @return
     */
    public boolean hasCycle(ListNode head) {
        if (head == null || head.next == null) {
            return false;
        }
        ListNode low = head.next;
        ListNode fast = head.next.next;
        while (low != null && fast != null) {
            if (low == fast) {
                return true;
            } else {
                low = low.next;
                if (fast.next==null){
                    return false;
                }
                fast = fast.next.next;
            }
        }
        return false;
    }
}
